import QtQuick 1.1
import com.nokia.meego 1.1
import com.nokia.extras 1.1

PageStackWindow {
    id: appWindow

    property string trip: tripList.count > 0 ? tripList.currentItem.tripName : ""
    initialPage: mainPage

    function openTrip(tripName)
    {
        console.log("opening trip: "+tripName)
        myData.openTrip(tripName)
        pageStack.push(Qt.resolvedUrl("TripPage.qml"))
    }

    function showSummary()
    {
        console.log("Showing Summary")
        pageStack.push(Qt.resolvedUrl("Summary.qml"))
    }

    function showReport()
    {
        console.log("Showing Report")
        pageStack.push(Qt.resolvedUrl("Report.qml"))
    }
    Page {
        id: mainPage
        tools: commonTools
        //orientationLock : PageOrientation.LockPortrait
        property bool empty: true
        ListView {
            id: tripList
            anchors.fill: parent
            model: myTrips
            rotation: 180
            highlight: Rectangle{ width:parent.width; smooth:true; opacity:0.5; color: "darkslategrey"; radius: 5}
            focus: true
            delegate:  Item {
                height: 60
                width: parent.width
                anchors.left: parent.left
                property variant tripName: modelData
                Label {
                    id: tripName_T
                    anchors {
                        left: parent.left
                        leftMargin: 10
                    }
                    width: parent.width
                    text: modelData; color: 'green'; font.pixelSize: 50;
                    elide: Text.ElideRight
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        tripList.currentIndex = index
                        openTrip(tripName_T.text)
                    }
                    //onDoubleClicked: console.log("Double Clicked")
                    rotation: 180
                }
                rotation: 180
            }
        }
        Label {
            visible: tripList.count == 0
            opacity: 0.3
            anchors.centerIn: parent
            text: "Welcome!!!\nStart by Adding a Trip"
            font.pixelSize: 32
        }
    }
    ToolBarLayout {
        id: commonTools
        visible: true
        ToolIcon { id:exit_B; iconId: "toolbar-close"; onClicked: Qt.quit(); }
        Button {
            id: trip_B
            text: "Add Trip"
            anchors.verticalCenter: exit_B.verticalCenter
            visible: pageStack.depth == 1
            onClicked: {
                tripDialog.edit = false; tripDialog.title = "Add Trip"; tripDialog.trip = "Trip"
                tripDialog.open() 
            }
        }
        //ToolIcon {
        //    id: del_B
        //    visible: pageStack.depth > 1
        //    iconSource: "image://theme/icon-s-common-remove-inverse"
        //    onClicked: pageStack.currentPage.del()
        //}
        ToolIcon {
            id: edit_B
            visible: pageStack.depth > 1
            iconSource: pageStack.currentPage.empty ? "image://theme/icon-m-toolbar-new-email" : "image://theme/icon-m-toolbar-new-email-white"
            onClicked: pageStack.currentPage.edit()
        }
        ToolIcon {
            id: add_B
            visible: pageStack.depth > 1
            iconSource: "image://theme/icon-m-toolbar-add-white"
            onClicked: pageStack.currentPage.add()
        }
        ToolIcon {
            id: home_B
            platformIconId: "toolbar-home"
            anchors.verticalCenter: exit_B.verticalCenter
            visible: pageStack.depth > 1
            onClicked: pageStack.pop()
        }
        ToolIcon {
            platformIconId: "toolbar-view-menu"
            anchors.right: (parent === undefined) ? undefined : parent.right
            onClicked: (myMenu.status === DialogStatus.Closed) ? myMenu.open() : myMenu.close()
        }
    }

    Menu {
        id: myMenu
        MenuLayout {
            MenuItem {
                text: qsTr("Export"); visible: pageStack.depth > 1;
                onClicked: {
                    myData.exportCSV(appWindow.trip)
                    banner0.text = "Trip Exported to folder ~/MyDocs/Documents"
                    banner0.show()
                }
            }
            MenuItem { text: qsTr("Summary"); visible: pageStack.depth > 1; onClicked: showSummary() }
            MenuItem { text: qsTr("Detailed Report"); visible: pageStack.depth > 1; onClicked: showReport() }
            MenuItem { text: qsTr("About");
                onClicked: pageStack.push(Qt.resolvedUrl("AboutDialog.qml"))
            }
        }
    }
    Component.onCompleted: {
        // "theme" comes magically from somewhere. No clue where.
        theme.inverted = true;
        theme.colorScheme = 10
        appWindow.platformStyle.cornersVisible = false
    }

    Dialog {
        id: tripDialog
        property alias title: titleLabel.text
        property alias trip: trip.text
        property bool edit: false
        title: Item {
            id: titleField
            height: tripDialog.platformStyle.titleBarHeight
            width: parent.width
            Image {
                id: supplement
                source: "image://theme/icon-l-contacts"
                height: parent.height - 10
                width: 75
                fillMode: Image.PreserveAspectFit
                anchors.leftMargin: 5
                anchors.rightMargin: 5
            }
    
            Label {
                id: titleLabel
                anchors.left: supplement.right
                anchors.verticalCenter: titleField.verticalCenter
                font.capitalization: Font.MixedCase
                color: "white"
                text: "Add Trip"
            }
    
            Image {
                id: closeButton
                anchors.verticalCenter: titleField.verticalCenter
                anchors.right: titleField.right
    
                source: "image://theme/icon-m-common-dialog-close"
    
                MouseArea {
                    id: closeButtonArea
                    anchors.fill: parent
                    onClicked:  { tripDialog.reject(); }
                }
            }
        }
    
        content:Item {
            height: 100
            Column {
                spacing:20
                Column {
                    spacing: 5
                    Label {
                        font.pixelSize: 23
                        color: "white"
                        text: "Trip Name"
                    }
                    TextField {
                        id: trip
                        text: "Trip"
                    }
                }
            }
        }
    
        buttons: ButtonRow {
            height:50
            platformStyle: ButtonStyle { }
            anchors {
                horizontalCenter: parent.horizontalCenter
                top: parent.bottom
                topMargin: 50
            }
            Button {id: b1; text: "OK"; onClicked: tripDialog.accept()}
            Button {id: b2; text: "Cancel"; onClicked: tripDialog.reject()}
        }

        onAccepted: {
            if(edit) {
                console.log("Renamed Trip: "+trip.text)
            }else {
                console.log("New Trip: "+trip.text)
                myData.newTrip(trip.text)
                pageStack.push(Qt.resolvedUrl("TripPage.qml"))
            }
        }
    }

    InfoBanner{
        id: banner0
        //text: textField.text
        timerEnabled: true
        //iconSource: checkBox2.checked ? "assets/system_banner_thumbnail.png" : ""
        timerShowTime: 3000
        anchors.centerIn: parent
    }

}
